﻿//PKM2 公用JS代码
var sBackColor = "#FFFF80";
var sMarkerMode = "FALSE";
var currentpos,timer; 
var comment_rng; 
var comment_obj;

document.ondblclick=initialize  
document.onmousedown=sc  
document.onmouseup=doMarker 
function initialize(){
	if(tInterval<10){tInterval=10;}
	timer=setInterval("scrollwindow()",tInterval);  
}
function sc(){  
	clearInterval(timer);  
}  
function scrollwindow(){  
	currentpos=document.documentElement.scrollTop;  
	window.scroll(0,++currentpos);  
	if (currentpos != document.documentElement.scrollTop)  
	sc();  
}  

function doMarker(){   
      var   oSel=document.selection.createRange()   
      var   sTemp=oSel.text  
      if(sMarkerMode == "TRUE" && sTemp.length>0){
      	oSel.execCommand("BackColor",false,sBackColor);
      	location.href='pkm2:savecontent'; 
      }
} 

function showcomment(){
	var o=event.srcElement;
	if(o.className!='inlinecomment'){return false;}
	if(o.title!=null && o.title!=""){
	comment_obj = o;
	var selText = o.innerText;
	if (selText.length > 20){selText = selText.substring(0,14) + "...";}	
	var commentbox=document.getElementById("commentbox");
	var commentcaption=document.getElementById("commentcaption");
	var inlinecomment=document.getElementById("inlinecomment");
	var commentcommand=document.getElementById("commentcommand");
	var x=window.event.clientX + document.documentElement.scrollLeft; 
	var y=window.event.clientY + document.documentElement.scrollTop + 5;
	commentbox.style.display="block";
	commentbox.style.posLeft=x; 
	commentbox.style.posTop=y;
	commentcaption.innerHTML = "批注: " + selText;
	inlinecomment.innerHTML = o.title;	
	inlinecomment.contentEditable=true;
	commentcommand.style.visibility="visible";	
	commentbox.style.display="block";		
	return true;	
}
}

function addcomment(){
comment_obj = null;
var sel = document.selection;	
document.getElementById("commentbox").style.display="none"; 
if (sel!=null && sMarkerMode != "TRUE"){		
	comment_rng = sel.createRange();	
	if (comment_rng!=null){
		selHtml = comment_rng.htmlText;
		selText = comment_rng.text;			
		if (selText.length > 16){selText = selText.substring(0,14) + "...";}
		if (selText.length > 0){
		var commentbox=document.getElementById("commentbox");
		var commentcaption=document.getElementById("commentcaption");
		var inlinecomment=document.getElementById("inlinecomment");
		var commentcommand=document.getElementById("commentcommand"); 
		var x= comment_rng.offsetLeft;
		var y= comment_rng.offsetTop + document.documentElement.scrollTop + 20;		
		commentbox.style.posLeft=x; 
		commentbox.style.posTop=y;	
		commentcommand.style.visibility="hidden";			
		commentbox.style.display="block";		
		commentcaption.innerHTML = "批注: " + selText;	
		inlinecomment.contentEditable=true;
		inlinecomment.innerHTML = "";		
		inlinecomment.focus;}
	  }
}
}

function savecomment(){
var commentbox=document.getElementById("commentbox");
var inlinecomment=document.getElementById("inlinecomment");
comment_html = inlinecomment.innerHTML;
comment_html = comment_html.replace(/\"/g, '');
if (comment_rng!=null && comment_html.length > 0){comment_rng.pasteHTML("<span class='inlinecomment' title='" + comment_html + "'>" + comment_rng.htmlText + "</span>");}
if (comment_obj!=null){
	if (comment_html.length > 0){comment_obj.setAttribute("title",comment_html);}
	else{comment_obj.outerHTML=comment_obj.innerHTML;}
}
commentbox.style.display = "none";
inlinecomment.innerHTML = "";
location.href='pkm2:savecontent'; 
}

function savecomment1(){
var commentbox=document.getElementById("commentbox");
var inlinecomment=document.getElementById("inlinecomment");
comment_html = inlinecomment.innerHTML;
comment_html = comment_html.replace(/\"/g, '');
if (comment_rng!=null && comment_html.length > 0){comment_rng.pasteHTML("<span class='inlinecomment' title='" + comment_html + "'>" + comment_rng.htmlText + "</span>");}
if (comment_obj!=null){
	if (comment_html.length > 0){comment_obj.setAttribute("title",comment_html);}
	else{comment_obj.outerHTML=comment_obj.innerHTML;}
}
commentbox.style.display = "none";
inlinecomment.innerHTML = "";
location.href='pkm2:savecontent'; 
}



function hidecomment(){
document.getElementById("commentbox").style.display = "none";
document.getElementById("inlinecomment").innerHTML = "";
}

function copycomment(){
  var objComment = document.getElementById("inlinecomment");
  window.clipboardData.setData('Text',objComment.innerText);
} 

function removecomment(){
  var objComment = document.getElementById("inlinecomment");
  objComment.innerHTML="";
  savecomment();
} 


var x,y,z; 
var down=false; 
var oEvent,oDragTarget; 
function startDrag(){ 
     oEvent=event.srcElement;   //获取焦点对象 
     oDragTarget=document.getElementById("commentbox"); 
     oEvent.setCapture();           //设置鼠标捕捉 
     z=oEvent.style.zIndex;         //取得原z轴位置 
     oEvent.style.zIndex=100;       //设定在最上层 
     x=event.offsetX;            //获取鼠标指针相对于触发事件的对象的x位置 
     y=event.offsetY;            //获取鼠标指针相对于触发事件的对象的y位置 
     down=true;                  //设置鼠标状态为按下状态 
 } 
function dragIt(){ 
    if(down&event.srcElement==oEvent){ 
        with(oDragTarget.style){ 
             position="absolute"; 
             posLeft=document.documentElement.scrollLeft+event.x-x; 
             posTop=document.documentElement.scrollTop+event.y-y; 
         } 
     } 
 } 
function stopDrag(){ 
     down=false; 
     oEvent.style.zIndex=z; 
     oEvent.releaseCapture();//释放鼠标捕捉 
 } 



function doZoom(){
  if (!window.event.ctrlKey) {return;}
  if(event.wheelDelta > 0){location.href='pkm2:zoom?y=up';}
  else{location.href='pkm2:zoom?y=down';}
}

function doPrint() {  
bdhtml=window.document.body.innerHTML;    
sprnstr="<!--startprint-->";    
eprnstr="<!--endprint-->";    
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);    
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
prnhtml="<div style='margin:10px 20px'>"+prnhtml+"</div>"    
window.document.body.innerHTML=prnhtml;    
window.print();    
} 

function SetFont(size){
	document.getElementById("logPanel").style.fontSize=size;
	location.href='pkm2:fontsize?key='+size;
}

function isValid(form){
    if(form.searchstring.value==""){
        alert("检索词不能为空！");
    return false;
    form.searchstring.focus();
    }
    else if((form.searchstring.value.length<2)||(form.searchstring.value.length>12)){
        alert("检索词长度不能 <2 或 >12 ！");
    return false;
    form.searchstring.focus();
    }
    else {
    return true;
    }
}

//-----PKM2 Navigation Menu -------------
function menuFix() {
	var sfEls = document.getElementById("nav").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++){
		sfEls[i].onmouseover=function(){this.className+=(this.className.length>0? " ": "") + "sfhover";}
		sfEls[i].onmouseout=function(){this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"),"");}
	}
}

//-----AccessKey For IE -------------
var NKey=new Array()
var KeyCode=new Array()
var KeyInit=false
//======== keyCode ============
KeyCode["1"]=49
KeyCode["2"]=50
KeyCode["3"]=51
KeyCode["["]=219
KeyCode["]"]=221
KeyCode[","]=188
KeyCode["."]=190
var EKey="abcdefghijklmnopqrstuwvxyz"
for (i=0;i<26;i++){
  KeyCode[EKey.substr(i,1)]=65+i
}

function PressKey(){
	if (document.all) {
		for (i=0;i<NKey.length;i++){
			if (window.event.altKey && window.event.keyCode==NKey[i].Code) {NKey[i].Object.blur();NKey[i].Object.click()}
   }
	}
}

function AccessKey(Code,Object){
 this.Code=Code
 this.Object=Object
}

function initAccessKey(){
 if (document.all && !KeyInit) {
    for (i=0;i<document.links.length;i++){
      if (document.links[i].accessKey) {
        NKey[NKey.length]=new AccessKey(KeyCode[document.links[i].accessKey.toLowerCase()],document.links[i])
      }
    }
 }
 KeyInit=true
}

//查找网页内宽度太大的图片进行缩放以及PNG纠正
 function ReImgSize(){
  for (i=0;i<document.images.length;i++){
  if (document.all){
	if (document.images[i].width>600)
	 {document.images[i].width="600"
       try{
	     document.images[i].outerHTML='<a href="'+document.images[i].src+'" target="_blank" title="在新窗口打开图片">'+document.images[i].outerHTML+'</a>'
  	 	}catch(e){}
  	}
   }
  else{
	if (document.images[i].width>600) {
	  document.images[i].title="在新窗口打开图片"
	  document.images[i].style.cursor="pointer"
	  document.images[i].onclick=function(e){window.open(this.src)}
	}
  }
  }
 }

//写入顶部Flash文件
function WriteHeadFlash(Path,Width,Height,Transparent){
	 var Temp,T=""
	 Temp='<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" id="FlashH" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" border="0" width="'+Width+'" height="'+Height+'">'
	 Temp+='<param name="movie" value="'+Path+'"/>'
	 Temp+='<param name="quality" value="High"/>'
	 Temp+='<param name="scale" value="ExactFit"/>'
	 if (Transparent) {Temp+=' <param name="wmode" value="transparent"/>';T='wmode="transparent"'}
	 Temp+='<embed src="'+Path+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="FlashH" width="'+Width+'" height="'+Height+'" quality="High"'+T+' scale="ExactFit"/>'
	 Temp+='</object>'
	 document.getElementById("FlashHead").innerHTML=Temp
}

//初始化JS代码
function initJS(){
	ReImgSize() //自动缩放代码
	menuFix()
	initAccessKey()  //转换AccessKey For IE
}


function killErrors(){
return true;
}
window.onerror = killErrors;
